withdrawalsController = BaseController.extend({
	template:'withdrawals',
	center:'提现',
	mainView: 'withdrawals',
	wallet_mobile:'',
	last_money:0,
	waitOn:function(){
		let self = this;
		self.partner = this.currentUser();
		Meteor.subscribe('wallets',{partner_id: parseInt(self.partner.id)},{});
		//Meteor.subscribe('wallets',{partner_id: this.partner.id},{});
	},
	onRendered:function(self){
		var myApp = new Framework7({
			modalTitle: "提示",
			modalButtonOk: "确定",
          	modalButtonCancel: "取消",
		});
		var $$ = Dom7;
		$$('.form-to-data').on('click', function () {
			var formData = myApp.formToData('#withdrawal');
			let m_code = $('#code').val();
			//需要验证输入密码是否正确
			let last_money = self.last_money;
			if(formData.money){
				if(formData.money < 0.01){
					myApp.alert('提现金额不得少于1分钱');
				}
				if (formData.money < last_money) {
					if(formData.money && m_code){
						myApp.confirm('确定提现？',function(){
							var post_data = {
								partner_id: self.partner.id,
								draw_fee: formData.money,
								code: m_code
							};
							self.post('partner/draw', post_data).then(function(res){
								if (res.data && ('OK' == res.data.state)) {
									myApp.alert('提现申请已发出，将在一个工作日到账');
									self.router.go('record');
								}else{
									myApp.alert('申请提现失败，请稍后再试');
								}
							}, function (errer) {
								console.log(errer)
								myApp.alert('申请提现失败，请稍后再试');
							})
						});
					}else{
						myApp.alert('请输入验证码');
					}
				} else {
					myApp.alert('提现金额不得超过账户余额，请重新输入');
				}
			}else{
				myApp.alert('请输入提现金额');
			}
		});
	},
	events: function (controller) {
		let self = this;
		return {
			'click #getPhone': function (e) {
				let code_state = false;
				let mobile = self.wallet_mobile;
				if(mobile){
					controller.get('user/mobile-code/' + mobile).then(function (res) {
						if (res.data.state == "OK") {
							$(e.currentTarget).attr('disabled','disabled');
                            countDown();
						}
					}, function (error) {
						console.log(error)
					});
				}else{
					controller.app.alert('请先绑定手机号');
				}

                function countDown() {
                    let index = 59;
                    let timeout = setInterval(function(){
                        $(e.currentTarget).html(index+'秒');
                        if(--index >= 0){
            
                        }else{
                         $(e.currentTarget).removeAttr('disabled');
                         $(e.currentTarget).html('获取验证码');
                        // clearInterval(id);
                        }
                    },1000);
                }
			},
		};
	},
	data:function(){
		var self = this;
		return{
			wallet : function(){
				var w = Wallets.findOne();
				self.wallet_mobile = w.mobile ? w.mobile : '';
				self.last_money = w.last_money ? w.last_money : 0;
				return w;
			},
		}
	},
})
